package com.gimi.cloud.jimi.manage.consumer.service.app.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.EntityVerification;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.app.ColumnDTO;
import com.gimi.cloud.jimi.manage.consumer.feign.AdAndColumnFeignService;
import com.gimi.cloud.jimi.manage.consumer.service.app.ColumnService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @auther loongs
 * @date 2017/12/27  14:42
 */
@Service
public class ColumnServiceImpl implements ColumnService {

    private static final Logger logger = LoggerFactory.getLogger(ColumnServiceImpl.class);
    private AdAndColumnFeignService adAndColumnFeignService;

    @Autowired
    public ColumnServiceImpl (AdAndColumnFeignService adAndColumnFeignService) {
        this.adAndColumnFeignService=adAndColumnFeignService;
    }

    @Override
    public Response getColumnList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ColumnDTO columnDTO = JsonUtil.jsonToBean(requestBody,ColumnDTO.class);
        if(StringUtil.isBlank(columnDTO.getPageSize())||StringUtil.isBlank(columnDTO.getCurrentPage())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.getColumnList(columnDTO);
    }

    @Override
    public Response getPreColumnProdList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ColumnDTO columnDTO = JsonUtil.jsonToBean(requestBody,ColumnDTO.class);
        if(StringUtil.isBlank(columnDTO.getPageSize())||StringUtil.isBlank(columnDTO.getCurrentPage())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.getPreColumnProdList(columnDTO);
    }

    @Override
    public Response addColumn(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        /*Map<String,String> map = JsonUtil.jsonToMap(requestBody);
        String columnSort = map.get("columnSort");
        if(columnSort.length() > 9){
            throw new MsgException(ResponseCodeEnum.COLUMN_SORT_IS_OUT_OF_LENGTH);
        }*/
        ColumnDTO columnDTO = JsonUtil.jsonToBean(requestBody,ColumnDTO.class);
        EntityVerification.checkByGroupsThrowsException(columnDTO,ColumnDTO.AddColumn.class);
        if(columnDTO.getProductList() == null || columnDTO.getProductList().size() < 1){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"请选择栏目对应的商品");
        }
        return adAndColumnFeignService.addColumn(columnDTO);
    }

    @Override
    public Response editColumn(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        /*Map<String,String> map=JsonUtil.jsonToMap(requestBody);
        String columnSort = map.get("columnSort");
        if(columnSort.length() > 9){
            throw new MsgException(ResponseCodeEnum.COLUMN_SORT_IS_OUT_OF_LENGTH);
        }*/
        ColumnDTO columnDTO = JsonUtil.jsonToBean(requestBody,ColumnDTO.class);
        EntityVerification.checkByGroupsThrowsException(columnDTO,ColumnDTO.AddColumn.class);
        if(columnDTO.getProductList() == null || columnDTO.getProductList().size() < 1){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"请选择栏目对应的商品");
        }
        return adAndColumnFeignService.editColumn(columnDTO);
    }

    @Override
    public Response gotoEditColumn(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ColumnDTO columnDTO = JsonUtil.jsonToBean(requestBody,ColumnDTO.class);
        if(StringUtil.isBlank(columnDTO.getColumnId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.gotoEditColumn(columnDTO);
    }

    @Override
    public Response deleteColumn(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ColumnDTO columnDTO = JsonUtil.jsonToBean(requestBody,ColumnDTO.class);
        if(StringUtil.isBlank(columnDTO.getColumnId())||StringUtil.isBlank(columnDTO.getColumnStatus())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.deleteColumn(columnDTO);
    }
}
